package com.zcxx.controller;

import com.zcxx.common.Result;
import com.zcxx.model.Employee;
import com.zcxx.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Spring Boot 通过 @RestController 或 @ResponseBody 注解自动处理返回值序列化。
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工管理", description = "管理系统用户的增删改查操作")
public class EmployeeApiController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加员工（包含照片上传）
     *
     * @param employee 员工信息
     * @param file     员工照片
     * @return 操作结果
     * @throws IOException 如果文件上传过程中发生错误
     */
    @PostMapping
    @ApiOperation(value = "添加员工", notes = "创建一个新员工，包含照片上传")
    @ApiResponses({
            @ApiResponse(code = 200, message = "员工信息插入成功"),
            @ApiResponse(code = 500, message = "员工信息插入失败")
    })
    public Result<String> addEmployee(
            @ApiParam(name = "employee", value = "员工信息", required = true) @ModelAttribute Employee employee,
            @ApiParam(name = "file", value = "员工照片", required = true) @RequestPart MultipartFile file) throws IOException {
        return Result.success(employeeService.saveEmployee(employee, file));
    }

    /**
     * 更新员工信息（包含照片上传）
     *
     * @param employee 员工信息
     * @param photo    员工照片
     * @return 操作结果
     * @throws IOException 如果文件上传过程中发生错误
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新员工信息", notes = "根据员工ID更新员工信息，包含照片上传")
    @ApiResponses({
            @ApiResponse(code = 200, message = "员工信息更新成功"),
            @ApiResponse(code = 404, message = "员工不存在"),
            @ApiResponse(code = 500, message = "员工信息更新失败")
    })
    public Result<String> updateEmployee(
            @ApiParam(name = "employee", value = "员工信息", required = true) @ModelAttribute Employee employee,
            @ApiParam(name = "photo", value = "员工照片", required = false) @RequestPart(required = false) MultipartFile photo
    ) throws IOException {
        return Result.success(employeeService.updateEmployee(employee, photo));
    }

    /**
     * 删除员工(包含照片删除)
     *
     * @param id 员工ID
     * @return 操作结果
     * @throws IOException 如果文件删除过程中发生错误
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工", notes = "根据员工ID删除员工，包含照片删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "员工信息删除成功"),
            @ApiResponse(code = 404, message = "员工不存在"),
            @ApiResponse(code = 500, message = "员工信息删除失败")
    })
    public Result<String> deleteEmployee(@ApiParam(name = "id", value = "员工ID", required = true) @PathVariable Integer id) throws IOException {
        return Result.success(employeeService.deleteEmployeeById(id));
    }

    /**
     * 查询所有员工
     *
     * @return 员工列表
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工", notes = "查询所有员工信息，返回员工列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功")
    })
    public Result<List<Employee>> getAllEmployees() {
        return Result.success(employeeService.list());
    }

    /**
     * 根据ID获取员工
     *
     * @param id 员工ID
     * @return 员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取员工", notes = "根据员工ID查询员工信息，返回员工详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "员工不存在")
    })
    public Result<Employee> getEmployeeById(@ApiParam(name = "id", value = "员工ID", required = true) @PathVariable Integer id) {
        return Result.success(employeeService.getById(id));
    }
}
